package cn.itcast.hotel;

import cn.itcast.hotel.config.HotelConstants;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class ElsIndexDemo {


    @Test
    public void createIndex() throws IOException {


        CreateIndexRequest request = new CreateIndexRequest("hotel");
//        request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
        Map map = new HashMap<>();
        map.put("number_of_shard",3);
        map.put("number_of_replicas",3);
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        boolean acknowledged = response.isAcknowledged();
        System.out.println("创建结果：" + acknowledged);

    }

    RestHighLevelClient client;

    @BeforeEach
    public void init() {
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://120.48.83.4:9200"),
                HttpHost.create("http://120.48.83.4:9201"),
                HttpHost.create("http://120.48.83.4:9202")
        ));
    }

    @AfterEach
    public void close() {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
